package com.workflow.action;

import java.util.List;
import java.util.Set;

import com.base.service.CardTypeService;
import com.base.service.DepartmentService;
import com.base.service.DictService;
import com.base.service.FlowworkitemService;
import com.base.service.PermissionService;
import com.common.bean.Cardmaintype;
import com.common.bean.Datadict;
import com.common.bean.Department;
import com.common.bean.Flowtemplatedetail;
import com.common.bean.Flowworkitem;
import com.common.bean.Personnel;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.workflow.bean.Salesdept;
import com.workflow.service.SalesdeptService;

public class SalesdeptManagerAction extends AbstractActionSupport implements Preparable{
	private static final long serialVersionUID = -1442456762139735258L;

	private SalesdeptService salesdeptService;
	
	private DepartmentService departmentService;
	
	private DictService dictService;
	
	private FlowworkitemService flowworkitemService;
	
	private CardTypeService cardTypeService;
	
	private PermissionService permissionService;
	
	private Salesdept salesdept;
	
	private String salesno;
	
	private PagerModel pm;
	
	private List<Department> departmentByDepartmentDeptcodelist;
	
	private List<Datadict> datadictByMarklist;
	
	private List<Datadict> datadictByUnitlist;
	
	private List<Datadict> datadictByPrilist;
	
	private List<Datadict> datadictByStatuslist;
	
	private List<Cardmaintype> cardmaintypelist;
	
	@SuppressWarnings("unchecked")
	public void prepare() throws Exception {
		departmentByDepartmentDeptcodelist = departmentService.findAllleafDepartment();
		cardmaintypelist = cardTypeService.findAllCardmaintype();
		datadictByUnitlist = dictService.findDictDetail(StringToken.DICT_WORK_UNIT);
		datadictByPrilist = dictService.findDictDetail(StringToken.DICT_WORK_PRI);
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String goAddSalesdept() throws Exception {
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addSalesdept() throws Exception {
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		Flowworkitem flowworkitem = null;
		/*
		 admin 账户为超级管理员用户！测试专用。		 
		 */
		String deptcode = "";
		if("admin".equals(personnel.getLoginname())){
			deptcode = personnel.getDepartment().getDeptcode();//TODO
		}else {
			deptcode = personnel.getDepartment().getDeptcode();
		}
		flowworkitem = flowworkitemService.finaFlowworkitem(deptcode
				, salesdept.getCardmaintype().getCardmaintypeno()/*类型一致*/
				, StringToken.DICT_FLOWFLAG_SALESDEPT);
		
		salesdept.setPersonnel(personnel);
		salesdept.setDepartmentByDeptcode(personnel.getDepartment());
		salesdept.setFlowworkitem(flowworkitem);
		
		Datadict datadictByMark = new Datadict();
		datadictByMark.setTypeid(StringToken.DICT_WORK_MARK_NO);
		salesdept.setDatadictByMark(datadictByMark);
		
		Object obj = null;
		Set<Flowtemplatedetail>	set = flowworkitem.getFlowtemplatedetails();
		for(Flowtemplatedetail flowtemplatedetail : set) {
			if(flowtemplatedetail.getSeq() == 1l) {
				//判断用户权限部门、角色
				if(flowtemplatedetail.getDepartment().equals(personnel.getDepartment())) {
					long ruleno = flowtemplatedetail.getRule().getRuleno();
					String staffcode = personnel.getStaffcode();
					
					obj = permissionService.findPersonneltorulelink(ruleno, staffcode);
				}
				
				salesdept.setFlowtemplatedetail(flowtemplatedetail);//默认1为保存，从1开始
				salesdept.setDatadictByStatus(flowtemplatedetail.getDatadictByWorkflowstatusno());//保存状态,默认
				salesdept.setDatadictByBranch(flowtemplatedetail.getDatadictByBranch());//是否分支 :流程1时，默认取1分支
			}
		}
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		salesdeptService.addSalesdept(salesdept);
		
		return SUCCESS;
	}
	
	public String findAllSalesdept() throws Exception {
		pm = salesdeptService.findSalesdeptForPager();
		return SUCCESS;
	}
	
	public String findSalesdept() throws Exception {
		salesdept = salesdeptService.findSalesdept(salesno);
		return SUCCESS;
	}
	
	public String delSalesdept() throws Exception {
		salesdeptService.removeSalesdept(salesno);
		return SUCCESS;
	}
	
	public String getSalesdeptForUpdate() throws Exception {
		salesdept = salesdeptService.findSalesdept(salesno);
		return SUCCESS;
	}
	
	public String updateSalesdept() throws Exception {
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		
		Salesdept sales = salesdeptService.findSalesdept(salesdept.getSalesno());
		
		Datadict datadictByMark = new Datadict();
		datadictByMark.setTypeid(StringToken.DICT_WORK_MARK_NO);
		sales.setDatadictByMark(datadictByMark);
		
		Object obj = null;
		long ruleno = sales.getFlowtemplatedetail().getRule().getRuleno();
		String staffcode = personnel.getStaffcode();
		
		obj = permissionService.findPersonneltorulelink(ruleno, staffcode);
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		sales.setDatadictByUnit(salesdept.getDatadictByStatus());
		sales.setDatadictByPri(salesdept.getDatadictByPri());
		sales.setLimitday(salesdept.getLimitday());
		sales.setRemark(salesdept.getRemark());
		
		salesdeptService.updateSalesdept(sales);
		return SUCCESS;
	}

	/**
	 * @return the cardmaintypelist
	 */
	public List<Cardmaintype> getCardmaintypelist() {
		return cardmaintypelist;
	}

	/**
	 * @param cardmaintypelist the cardmaintypelist to set
	 */
	public void setCardmaintypelist(List<Cardmaintype> cardmaintypelist) {
		this.cardmaintypelist = cardmaintypelist;
	}

	/**
	 * @return the cardTypeService
	 */
	public CardTypeService getCardTypeService() {
		return cardTypeService;
	}

	/**
	 * @param cardTypeService the cardTypeService to set
	 */
	public void setCardTypeService(CardTypeService cardTypeService) {
		this.cardTypeService = cardTypeService;
	}

	/**
	 * @return the datadictByMarklist
	 */
	public List<Datadict> getDatadictByMarklist() {
		return datadictByMarklist;
	}

	/**
	 * @param datadictByMarklist the datadictByMarklist to set
	 */
	public void setDatadictByMarklist(List<Datadict> datadictByMarklist) {
		this.datadictByMarklist = datadictByMarklist;
	}

	/**
	 * @return the datadictByPrilist
	 */
	public List<Datadict> getDatadictByPrilist() {
		return datadictByPrilist;
	}

	/**
	 * @param datadictByPrilist the datadictByPrilist to set
	 */
	public void setDatadictByPrilist(List<Datadict> datadictByPrilist) {
		this.datadictByPrilist = datadictByPrilist;
	}

	/**
	 * @return the datadictByStatuslist
	 */
	public List<Datadict> getDatadictByStatuslist() {
		return datadictByStatuslist;
	}

	/**
	 * @param datadictByStatuslist the datadictByStatuslist to set
	 */
	public void setDatadictByStatuslist(List<Datadict> datadictByStatuslist) {
		this.datadictByStatuslist = datadictByStatuslist;
	}

	/**
	 * @return the datadictByUnitlist
	 */
	public List<Datadict> getDatadictByUnitlist() {
		return datadictByUnitlist;
	}

	/**
	 * @param datadictByUnitlist the datadictByUnitlist to set
	 */
	public void setDatadictByUnitlist(List<Datadict> datadictByUnitlist) {
		this.datadictByUnitlist = datadictByUnitlist;
	}

	/**
	 * @return the departmentByDepartmentDeptcodelist
	 */
	public List<Department> getDepartmentByDepartmentDeptcodelist() {
		return departmentByDepartmentDeptcodelist;
	}

	/**
	 * @param departmentByDepartmentDeptcodelist the departmentByDepartmentDeptcodelist to set
	 */
	public void setDepartmentByDepartmentDeptcodelist(
			List<Department> departmentByDepartmentDeptcodelist) {
		this.departmentByDepartmentDeptcodelist = departmentByDepartmentDeptcodelist;
	}

	/**
	 * @return the departmentService
	 */
	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	/**
	 * @param departmentService the departmentService to set
	 */
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	/**
	 * @return the dictService
	 */
	public DictService getDictService() {
		return dictService;
	}

	/**
	 * @param dictService the dictService to set
	 */
	public void setDictService(DictService dictService) {
		this.dictService = dictService;
	}

	/**
	 * @return the flowworkitemService
	 */
	public FlowworkitemService getFlowworkitemService() {
		return flowworkitemService;
	}

	/**
	 * @param flowworkitemService the flowworkitemService to set
	 */
	public void setFlowworkitemService(FlowworkitemService flowworkitemService) {
		this.flowworkitemService = flowworkitemService;
	}

	/**
	 * @return the permissionService
	 */
	public PermissionService getPermissionService() {
		return permissionService;
	}

	/**
	 * @param permissionService the permissionService to set
	 */
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	/**
	 * @return the pm
	 */
	public PagerModel getPm() {
		return pm;
	}

	/**
	 * @param pm the pm to set
	 */
	public void setPm(PagerModel pm) {
		this.pm = pm;
	}

	/**
	 * @return the salesdept
	 */
	public Salesdept getSalesdept() {
		return salesdept;
	}

	/**
	 * @param salesdept the salesdept to set
	 */
	public void setSalesdept(Salesdept salesdept) {
		this.salesdept = salesdept;
	}

	/**
	 * @return the salesdeptService
	 */
	public SalesdeptService getSalesdeptService() {
		return salesdeptService;
	}

	/**
	 * @param salesdeptService the salesdeptService to set
	 */
	public void setSalesdeptService(SalesdeptService salesdeptService) {
		this.salesdeptService = salesdeptService;
	}

	/**
	 * @return the salesno
	 */
	public String getSalesno() {
		return salesno;
	}

	/**
	 * @param salesno the salesno to set
	 */
	public void setSalesno(String salesno) {
		this.salesno = salesno;
	}
	
	
}
